export function useDragMethods () {
  /**
   * 设置节点的排序值
   * @param children
   */
  function setSort (children: any) {
    if (!children) return
    for (let i = 0; i < children.length; i++) {
      children[i].sort = i
      if (children[i].children) {
        setSort(children[i].children)
      }
    }
  }

  /**
   * 获取指定节点的父节点ID
   *
   * @param node 目标节点
   * @param currentNode 当前节点
   * @returns 父节点ID，如果未找到父节点则返回null
   */
  function getParentNode (node: any, currentNode: any): any {
    if (currentNode.children) {
      for (let i = 0; i < currentNode.children.length; i++) {
        if (currentNode.children[i].id == node.id) {
          return currentNode.id
        }
        if (
          currentNode.children[i].children &&
          currentNode.children[i].children.length > 0
        ) {
          let re = getParentNode(node, currentNode.children[i])
          if (re) {
            return re
          }
        }
      }
    }
    return null
  }

  /**
   * 重置父级ID
   *
   * @param dList 数据列表
   * @param id 目标ID
   * @param pId 新的父级ID
   */
  function resetParentId (dList: any, id: any, pId: any) {
    for (let i = 0; i < dList.length; i++) {
      if (dList[i].id == id) {
        dList[i].parentId = pId
      }
      if (dList[i].children && dList[i].children.length > 0) {
        resetParentId(dList[i].children, id, pId)
      }
    }
  }
  return {
    setSort,
    getParentNode,
    resetParentId
  }
}
